package com.yuewen.permissioncheck

import android.content.Intent
import android.util.Log
import androidx.localbroadcastmanager.content.LocalBroadcastManager

/**
 * @author zhanglulu
 */
object PermissionCheckManager {
  const val TAG = "PermissionCheck"
  const val PERMISSION_CHECK_RECEIVER = "permission_check_receiver"

  val list = arrayListOf<Item>()

  /**
   * 通知有获取权限的路径
   *
   * @param permissionDescription 权限描述
   * @param stackTrace            堆栈
   */
  fun notifyBroadcastMsg(permissionDescription: String, stackTrace: Throwable?) {
    val stackTraceStr = Log.getStackTraceString(stackTrace)
    val msg = "⚠️⚠️⚠️ 警告️ | 正在获取:$permissionDescription | called stack trace:\n$stackTraceStr"
    Log.w(TAG, msg)
    list.add(Item(System.currentTimeMillis(), permissionDescription, stackTraceStr))
    LocalBroadcastManager.getInstance(AppContentProvider.app).sendBroadcast(Intent(PERMISSION_CHECK_RECEIVER))
  }
}

data class Item (val time: Long, val permissionDescription: String, val stackTraceStr: String)
